package com.mycar.pajero.common;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author Administrator -- Yinpeng
 * @connect: einpare@gmail.com
 *
 *  分别可以获取Date Time Week
 */
public class SaxDemo {
    public static Date parseRFC3339Date(String datestring)
            throws java.text.ParseException, IndexOutOfBoundsException {
        Date d = new Date();
        // if there is no time zone, we don't need to do any special parsing.
        if (datestring.endsWith("Z")) {
            try {
                SimpleDateFormat s = new SimpleDateFormat(
                        "yyyy-MM-dd'T'HH:mm:ss'Z'");// spec for RFC3339
                d = s.parse(datestring);
            } catch (java.text.ParseException pe) {// try again with optional
                // decimals
                SimpleDateFormat s = new SimpleDateFormat(
                        "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'");// spec for RFC3339
                // (with fractional
                // seconds)
                s.setLenient(true);
                d = s.parse(datestring);
            }
            return d;
        }
        // step one, split off the timezone.
        String firstpart = datestring.substring(0, datestring.lastIndexOf('-'));
        String secondpart = datestring.substring(datestring.lastIndexOf('-'));
        // step two, remove the colon from the timezone offset
        secondpart = secondpart.substring(0, secondpart.indexOf(':'))
                + secondpart.substring(secondpart.indexOf(':') + 1);
        datestring = firstpart + secondpart;
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");// spec
        // for
        // RFC3339
        try {
            d = s.parse(datestring);
        } catch (java.text.ParseException pe) {// try again with optional
            // decimals
            s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");// spec
            // for
            // RFC3339
            // (with
            // fractional
            // seconds)
            s.setLenient(true);
            d = s.parse(datestring);
        }
        return d;
    }
    public static String getSimpleDateFormat(Date d) {
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss EEE", Locale.CHINA);
        String result = format.format(d);
        return result;
    }
    public static String getDate(Date d) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd",
                Locale.CHINA);
        String result = format.format(d);
        return result;
    }
    public static String getTime(Date d) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss", Locale.CHINA);
        String result = format.format(d);
        return result;
    }
    public static String getWeek(Date d) {
        SimpleDateFormat format = new SimpleDateFormat("EEE", Locale.CHINA);
        String result = format.format(d);
        return result;
    }
    public static String getSimpleDateFormat(String date) {
        String result = null;
        Date d = new Date(date);
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss EEE", Locale.CHINA);
        result = dateFormat.format(d);
        return result;
    }
    public static String getDate(String date) {
        String result = null;
        Date d = new Date(date);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
                Locale.CHINA);
        result = dateFormat.format(d);
        return result;
    }
    public static String getTime(String date) {
        String result = null;
        Date d = new Date(date);
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss",
                Locale.CHINA);
        result = dateFormat.format(d);
        return result;
    }
    public static String getWeek(String date) {
        String result = null;
        Date d = new Date(date);
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE", Locale.CHINA);
        result = dateFormat.format(d);
        return result;
    }


    /**
     * 将时间戳转为RFC3339标准时间格式
     * @param timeStamp
     * @return
     */
    public static String timeStampToRfc3339(long timeStamp) {
        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String formatDate = simpleDateFormat.format(new Date(timeStamp));
        return formatDate;
    }


    // some testing stuff in main()
    public static void main(String[] args) throws java.text.ParseException {

        String atom = timeStampToRfc3339(System.currentTimeMillis());
        Date rss = new Date();
        String current = atom;

        Date d = parseRFC3339Date(atom);
        System.out.println(getSimpleDateFormat(d));
        System.out.println(getDate(d));
        System.out.println(getTime(d));
        System.out.println(getWeek(d));

        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<< getSimpleDateFormat");
        System.out.println(getSimpleDateFormat(rss));
        System.out.println(getDate(rss));
        System.out.println(getTime(rss));
        System.out.println(getWeek(rss));

        System.out.println("<<<<<<<<<<<<<<<<<<<<<<<< timeStampToRfc3339");
        System.out.println(atom);
    }
}

